package com.xjcs.saplingplan.dialog

import android.content.Context
import com.lxj.xpopup.core.CenterPopupView
import com.xjcs.saplingplan.R
import com.xjcs.saplingplan.adapter.CountdownCatePopAdapter
import com.xjcs.saplingplan.const.setSingleClick
import com.xjcs.saplingplan.const.toToast
import com.xjcs.saplingplan.databinding.PopCountdownCateBinding
import com.xjcs.saplingplan.room.bean.countdown.PlanCateBean
import com.xjcs.saplingplan.ui.activity.countdown.CountdownCateActivity

class CountdownCatePop(
    private val context: Context,
    private val curTypeName: String,
    private val cateList: MutableList<PlanCateBean>,
    private val resultCallback: (String) -> Unit
) : CenterPopupView(context) {

    private lateinit var remindAdapter: CountdownCatePopAdapter
    private lateinit var binding: PopCountdownCateBinding

    override fun getImplLayoutId(): Int {
        return R.layout.pop_countdown_cate
    }

    override fun onCreate() {
        super.onCreate()
        binding = PopCountdownCateBinding.bind(contentView)
        initRecyclerView()
        initListener()
    }

    private fun initListener() {
        binding.tvCancel.setSingleClick {
            dialog.dismiss()
        }
        binding.tvManagerCate.setSingleClick {
            CountdownCateActivity.startActivity(context)
        }
        binding.tvSure.setSingleClick {
            val selectCateName = remindAdapter.getSelectedCateName()
            if(selectCateName.isEmpty()){
                "请选择分类".toToast()
                return@setSingleClick
            }
            resultCallback.invoke(selectCateName)
            dialog.dismiss()
        }
    }

    private fun initRecyclerView() {
        remindAdapter =
            CountdownCatePopAdapter(context, cateList, curTypeName)
        binding.recyclerView.adapter = remindAdapter
    }

}